<?php
	/*** This is the router. It will route the requests to the respective handler
	The router gets request and serves the page ***/

	/**
	 * 
	 * autoload function is called whenever a new class is instatiated
	 * @param $classname: name of the class to be loaded
	 */
	/*
	function __autoload($classname)	{
		//parse out file name where class should be located
		list($filename, $suffix) = explode('_', $classname);
			
		//compose file name
		$file = $ubi['SERVER']['SERVER_ROOT'] . '/controller/' . strtoupper($filename) . '.php';
							
		//fetch file
		if (file_exists($file))	{
			//include file
			include_once($file);
		} else {
			//file doesn't exist
			die("file $file contains class $classname does not exist");
		}
	}*/
	
	//fetch the passed request
	$request = $_SERVER['QUERY_STRING'];
	
	//parse the page request and other variables
	$parsed = explode('&', $request);
	
	//the request ( the page )
	$page = array_shift($parsed);
	
	//the rest of the array are get statements. Get them out
	$getVars = array();
	foreach($parsed as $argument) {
		list($variable, $value) = explode('=', $argument);
		$getVars[$variable] = $value;
	}
	
	//optain the path for the appropriate controller
	if ($page == '') {
		$page = "home";
	}
	
	$target = $ubi['SERVER']['SERVER_ROOT'] . '/admin/controller/ctl' . ucfirst(strtolower($page)) . '.php';
	
	//get target
	if (file_exists($target)) {
		include_once($target);

		//get the class name (have a naming conventrion)
		$class = ucfirst($page) . 'Controller';
		
		//instantiate the appropriate class
		if (class_exists($class)){
			$controller = new $class();
			
			//after we have the controller instantiated, call the default main method
			//pass in the getVars
			$controller->main($getVars);
	
		} else {
			die('class does not exist');
		}
	} else {
		die('page does not exist. bla bla bla');
	}
?>